package com.beasts.ant.page.entity.content;

import com.beasts.ant.page.utils.unique.UUIDUtil;
import lombok.Data;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;

import javax.persistence.*;
import javax.servlet.http.HttpServletRequest;
import java.io.Serializable;
import java.util.Date;
import java.util.Map;

/**
 * 文章管理
 *
 * @author Locker
 * @date 25/06/2018 9:00 AM
 * @since 1.0
 */
@Data
@Entity
@Table(name = "ap_article")
public class ApArticle implements Serializable {

    /**
     * 唯一 id
     */
    @Id
//    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", nullable = false, length = 32)
    private String id;

    /**
     * 标题
     */
    @Column(name = "title", nullable = false, length = 200)
    private String title;

    /**
     * 内容
     * <p>mediumtext 可存储16M</p>
     */
    @Column(name = "content", columnDefinition = "mediumtext")
    private String content;

    /**
     * 创建者
     */
    @Column(name = "creator", nullable = false, length = 32)
    private String creator;

    /**
     * 类别
     */
    @Column(name = "type", nullable = false, length = 32)
    private String type;

    /**
     * 附件 id
     */
    @Column(name = "attatch_blob_id", length = 32)
    private String attatchBlobId;

    /**
     * 文章编号
     */
    @Column(name = "sno", nullable = false, length = 32)
    private String sno;

    /**
     * 作者
     */
    @Column(name = "author", nullable = false, length = 32)
    private String author;

    /**
     * 修改者
     */
    @Column(name = "mendor", nullable = false, length = 32)
    private String mendor;

    /**
     * 链接地址（文章可链接到 url）
     */
    @Column(name = "href", length = 200)
    private String href;

    /**
     * 访问量
     */
    @Column(name = "visit_capacity")
    private Integer visitCapacity;

    /**
     * 创建时间
     */
    @Column(name = "create_time", updatable = false)
    @Temporal(TemporalType.TIMESTAMP)
    @CreatedDate
    private Date createTime;

    /**
     * 写作时间
     */
    @Column(name = "wrote_time")
    @Temporal(TemporalType.TIMESTAMP)
    private Date wroteTime;

    /**
     * 修改时间
     */
    @Column(name = "mend_time")
    @Temporal(TemporalType.TIMESTAMP)
    @LastModifiedDate
    private Date mendTime;

    /**
     * 生效日期
     */
    @Column(name = "valid_data")
    @Temporal(TemporalType.TIMESTAMP)
    private Date validDate;

    /**
     * 失效日期
     */
    @Column(name = "expire_date")
    @Temporal(TemporalType.TIMESTAMP)
    private Date expireDate;

    /**
     * 文章导读
     */
    @Column(name = "review", length = 1024)
    private String review;

    public ApArticle() {}

    public ApArticle(HttpServletRequest req) {
        if (null == req.getParameter("id")) {
            this.id = UUIDUtil.get(true);
        } else {
            this.id = req.getParameter("id");
        }
        this.title = req.getParameter("title");
        this.sno = req.getParameter("sno");
        this.href = req.getParameter("href");
        this.content = req.getParameter("content");
        this.review = req.getParameter("review");

    }

    static Object isValidOrNull(Object obj) {
        return obj==null ? null : obj;
    }

}
